﻿var userModel = null;

var camSchoolApp = angular.module("camSchoolApp", ["ngResource", "ngRoute"]);

camSchoolApp.run(function ($rootScope) {
})


camSchoolApp.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);

    $routeProvider.when("/startPage.html", {
        templateUrl: "View/loginPage.html",
        controller: "loginController"
    });

    $routeProvider.when("/mainPage", {
        templateUrl: "View/mainPage.html",
        controller: "mainController"
    });

    $routeProvider.when("/addEmployee", {
        templateUrl: "View/addEmployee.html",
        controller: "addEmployeeController"
    });

    //$routeProvider.when("/logOut", {
    //    templateUrl: "View/loginPage.html",
    //    controller: "logoutController"
    //});

    $routeProvider.otherwise({
        redirectTo: "/startPage.html",
        templateUrl: "View/loginPage.html",
        controller: "logoutController"
    });
});



function displayErrorLogin(title, errorMessage) {
    //notify the user 
    $.Notify({
        content: errorMessage,
        caption: title,
        style: { background: 'red', color: 'white', font: 'bold' },
        height: 50,
        shadow: true,
        timeout: 5000
    });
}

function validateInputValueOnUserEdit() {
    var isValid = true;
    if ($("#usernameEditUserInput").val() === "") {
        $("#usernameEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#usernameEditUser").removeClass("error-state");
    }


    if ($("#firstnameEditUserInput").val() === "") {
        $("#firstnameEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#firstnameEditUser").removeClass("error-state");
    }

    if ($("#lastnameEditUserInput").val() === "") {
        $("#lastnameEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#lastnameEditUser").removeClass("error-state");
    }

    if ($("#emailEditUserInput").val() === "") {
        $("#emailEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#emailEditUser").removeClass("error-state");
    }

    if ($("#streetEditUserInput").val() === "") {
        $("#streetEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#streetEditUser").removeClass("error-state");
    }

    if ($("#cityEditUserInput").val() === "") {
        $("#cityEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#cityEditUser").removeClass("error-state");
    }

    if ($("#streetEditUserInput").val() === "") {
        $("#streetEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#streetEditUser").removeClass("error-state");
    }

    if ($("#countryEditUserInput").val() === "") {
        $("#countryEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#countryEditUser").removeClass("error-state");
    }

    if ($("#cellPhoneEditUserInput").val() === "") {
        $("#cellPhoneEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#cellPhoneEditUser").removeClass("error-state");
    }

    if ($("#workPhoneEditUserInput").val() === "") {
        $("#workPhoneEditUser").addClass("error-state");
        isValid = false;
    } else {
        $("#workPhoneEditUser").removeClass("error-state");
    }

    if (!isValid) {
        displayErrorLogin("Erreur", "Vos identifiants sont incorrect.");
    } else {
        //Upadate Model TODO
    }
}

function setInputValueOnUserEdit() {
    $("#usernameEditUserInput").attr("value", userModel.username);
    $("#firstnameEditUserInput").attr("value", userModel.firstname);
    $("#lastnameEditUserInput").attr("value", userModel.lastname);
    $("#emailEditUserInput").attr("value", userModel.email);
    $("#streetEditUserInput").attr("value", userModel.street);
    $("#cityEditUserInput").attr("value", userModel.city);
    $("#streetEditUserInput").attr("value", userModel.street);
    $("#countryEditUserInput").attr("value", userModel.country);
    $("#cellPhoneEditUserInput").attr("value", userModel.cellPhone);
    $("#workPhoneEditUserInput").attr("value", userModel.workPhone);
}



function addNewEmployee() {
    var employeeTemplate = '<div id="userEditTemplate" ng-controller="mainController">' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Utilisateur</label>' +
                                    '<div id="usernameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="usernameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Mot de passe</label>' +
                                    '<div id="passwordEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="passwordEditUserInput" title="Entrez votre nouveau mot de passe ici" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Prenom</label>' +
                                    '<div id="firstnameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="firstnameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nom</label>' +
                                    '<div id="lastnameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="lastnameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Email</label>' +
                                    '<div id="emailEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="emailEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nom de la Rue</label>' +
                                    '<div id="streetEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="streetEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Ville</label>' +
                                    '<div id="cityEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="cityEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Pays</label>' +
                                    '<div id="countryEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="countryEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nr. de Telephone</label>' +
                                    '<div id="cellPhoneEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="cellPhoneEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nr. de Telephone Travail</label>' +
                                    '<div id="workPhoneEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="workPhoneEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<div id="profilPictureEditUser" class="input-control file textBoxLogin">' +
                                        '<input id="profilPictureEditUserInput" type="file" />' +
                                        '<button class="btn-file"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<h6 class="textLegendLogin">*Changer votre photo de profil ici</h6>' +
                                '<button id="saveEdit" onClick="validateInputValueOnUserEdit()" class="small dialogFont">Sauvegarder</button>'
    '</div>';

    $.Dialog({
        overlay: true,
        shadow: true,
        flat: true,
        title: '<label class="titleEditUser">Ajouter un employé</label>',
        overlayClickClose: false,
        content: $(employeeTemplate)
    });

    loadingWindow = $(".window").css("width", "400px");
    loadingWindow = $(".window").css("height", "610px");

    $(".caption").attr("id", "newEmployeeCaption");
}

function editUserAccount() {
    var userEditTemplate = '<div id="userEditTemplate" ng-controller="mainController">' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Utilisateur</label>' +
                                    '<div id="usernameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="usernameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Mot de passe</label>' +
                                    '<div id="passwordEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="passwordEditUserInput" title="Entrez votre nouveau mot de passe ici" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Prenom</label>' +
                                    '<div id="firstnameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="firstnameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nom</label>' +
                                    '<div id="lastnameEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="lastnameEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Email</label>' +
                                    '<div id="emailEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="emailEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nom de la Rue</label>' +
                                    '<div id="streetEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="streetEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Ville</label>' +
                                    '<div id="cityEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="cityEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Pays</label>' +
                                    '<div id="countryEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="countryEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nr. de Telephone</label>' +
                                    '<div id="cellPhoneEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="cellPhoneEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<label class="labelLogin">Nr. de Telephone Travail</label>' +
                                    '<div id="workPhoneEditUser" class="input-control text textBoxLogin">' +
                                        '<input id="workPhoneEditUserInput" type="text" />' +
                                        '<button class="btn-clear"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<div class="loginLabelTextBoxEditUser">' +
                                    '<div id="profilPictureEditUser" class="input-control file textBoxLogin">' +
                                        '<input id="profilPictureEditUserInput" type="file" />' +
                                        '<button class="btn-file"></button>' +
                                     '</div>' +
                                '</div>' +
                                '<h6 class="textLegendLogin">*Changer votre photo de profil ici</h6>' +
                                '<button id="saveEdit" onClick="validateInputValueOnUserEdit()" class="small dialogFont">Sauvegarder</button>'
                            '</div>';

    $.Dialog({
        overlay: true,
        shadow: true,
        flat: true,
        title: '<label class="titleEditUser">Modifier votre compte</label>',
        overlayClickClose: false,
        content: $(userEditTemplate)
    });

    loadingWindow = $(".window").css("width", "400px");
    loadingWindow = $(".window").css("height", "610px");

    $(".caption").attr("id", "userEditCaption");

    setInputValueOnUserEdit();
}

function validateInputValueAndSaveEmployee() {

    var aa = {
        "username": "sihddhjkl",
        "password": "adwrgdd",
        "userPrivileg": "addd",
        "firstname": "addd",
        "lastname": "addd",
        "street": "addd",
        "city": "addd",
        "country": "addd",
        "email": "addd@ya.de",
        "cellPhone": "0047276632",
        "workPhone": "2239878788"
    };
    
    var user = JSON.stringify(aa);

        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: "http://localhost:1984/CamSchoolWebService/rest/authentificationService/createNewUser",
            dataType: "json",
            data: user,
            success: function (data, textStatus, jqXHR) {
                alert('Wine created successfully');
                
                alert(val(data));
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('addWine error: ' + textStatus);
            }
        });

  

}

function initTreeView() {
    $("#personalTreeView").treeview();
}